using System;
using System.Reflection;
using System.Runtime.Serialization;

namespace SharpCore.NullableTypes
{
	/// <summary>
	/// Wraps the <see cref="System.Int64"/> value type in an object so that references can be null.
	/// </summary>
	[Serializable]
	public sealed class NullableLong : INullable
	{
		/// <summary>
		/// The <see cref="System.Int64"/> value this instance represents.
		/// </summary>
		private long longValue;

		/// <summary>
		/// Initializes a new instance of the NullableLong class.
		/// </summary>
		public NullableLong()
		{
			this.longValue = 0;
		}

		/// <summary>
		/// Initializes a new instance of the NullableLong class with a <see cref="System.Int64"/> value.
		/// </summary>
		/// <param name="longValue">The <see cref="System.Int64"/> that this instance represents.</param>
		public NullableLong(long longValue)
		{
			this.longValue = longValue;
		}

		/// <summary>
		/// Initializes a new instance of the NullableLong class with a <see cref="System.String"/> that represents a <see cref="System.Int64"/>.
		/// </summary>
		/// <param name="longValue">The string representation of a <see cref="System.Int64"/> value.</param>
		public NullableLong(string longValue)
		{
			this.longValue = Int64.Parse(longValue);
		}

		/// <summary>
		/// Creates a NullableLong that is serializable with the specified SerializationInfo and StreamingContext.
		/// </summary>
		/// <param name="info">A SerializationInfo object containing the information required to serialize the NullableLong.</param>
		/// <param name="context">A StreamingContext object containing the source and destination of the serialized stream associated with the NullableLong.</param>
		private NullableLong(SerializationInfo info, StreamingContext context)
		{
			this.longValue = (long) info.GetInt64("Value");
		}

		/// <summary>
		/// Returns the data needed to serialize the NullableLong.
		/// </summary>
		/// <param name="info">A SerializationInfo object containing the information required to serialize the NullableLong.</param>
		/// <param name="context">A StreamingContext object containing the source and destination of the serialized stream associated with the NullableLong.</param>
		void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue("Value", longValue);
		}

		/// <summary>
		/// Gets or Sets the <see cref="System.Int64"/> value this instance represents.
		/// </summary>
		public long LongValue
		{
			get { return longValue; }
			set { longValue = value; }
		}

		/// <summary>
		/// Converts a <see cref="System.Int64"/> to a NullableLong.
		/// </summary>
		/// <param name="longValue">The <see cref="System.Int64"/> to be converted.</param>
		/// <returns>A NullableLong instance that wraps the <see cref="System.Int64"/> value.</returns>
		public static implicit operator NullableLong(long longValue)
		{
			return new NullableLong(longValue);
		}

		/// <summary>
		/// Checks two NullableLong instances for equality.
		/// </summary>
		/// <param name="a">A NullableLong instance to test for equality.</param>
		/// <param name="b">A NullableLong instance to test for equality.</param>
		/// <returns><b>true</b> if both instances are the same; otherwise <b>false</b>.</returns>
		public static bool operator ==(NullableLong a, NullableLong b)
		{
			if (a == null && b == null)
			{
				return true;
			}
			else if (a == null || b == null)
			{
				return false;
			}
			else
			{
				return (a.LongValue == b.LongValue);
			}
		}

		/// <summary>
		/// Checks two NullableLong instances for inequality.
		/// </summary>
		/// <param name="a">A NullableLong instance to test for inequality.</param>
		/// <param name="b">A NullableLong instance to test for inequality.</param>
		/// <returns><b>true</b> if each instance is different; otherwise <b>false</b>.</returns>
		public static bool operator !=(NullableLong a, NullableLong b)
		{
			if (a == null && b == null)
			{
				return false;
			}
			else if (a == null || b == null)
			{
				return true;
			}
			else
			{
				return (a.LongValue != b.LongValue);
			}
		}

		/// <summary>
		/// Compares this instance to a specified object and returns an indication of their relative values.
		/// </summary>
		/// <param name="obj">An object to compare to this instance, or a null reference.</param>
		/// <returns>A signed integer that indicates the relative order of this instance and obj.</returns>
		public int CompareTo(object obj)
		{
			if (obj == null)
			{
				return 1;
			}
			else if (obj is NullableLong)
			{
				NullableLong nullableLong = obj as NullableLong;
				return longValue.CompareTo(nullableLong.LongValue);
			}
			else if (obj is long)
			{
				return longValue.CompareTo(obj);
			}
			else
			{
				throw new ArgumentException("The specified object was neither a long nor a NullableLong instance.");
			}
		}

		/// <summary>
		/// Returns a value indicating whether this instance is equal to a specified object.
		/// </summary>
		/// <param name="obj">An object to compare to this instance.</param>
		/// <returns><b>true</b> if obj is a NullableLong and has the same value as this instance; otherwise, <b>false</b>.</returns>
		public override bool Equals(object obj)
		{
			return longValue.Equals(obj);
		}

		/// <summary>
		/// Returns the hash code for this instance.
		/// </summary>
		/// <returns>A hash code for the current NullableLong.</returns>
		public override int GetHashCode()
		{
			return longValue.GetHashCode();
		}

		/// <summary>
		/// Converts the value of this instance to its equivalent string representation.
		/// </summary>
		/// <returns>The string representation of the value of this instance.</returns>
		public override string ToString()
		{
			return longValue.ToString();
		}
	}
}
